package cn.sc.summer.construction.service;

import cn.hutool.core.util.StrUtil;
import cn.sc.summer.daoapi.dao.SysUserInfoMapper;
import cn.sc.summer.domain.service.SysService;
import cn.sc.summer.file.util.FileUtil;
import cn.sc.summer.exception.exceptions.CommonException;
import cn.sc.summer.feign.client.OssFeignClient;
import cn.sc.summer.file.util.ImageUtil;
import cn.sc.summer.protocol.form.QRCodeForm;
import cn.sc.summer.protocol.form.WebFileForm;
import cn.sc.summer.token.po.SysUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 类名：sys接口实现类
 *
 * @author a-xin
 * @date 2024/1/15 14:04
 */
@Slf4j
@Service
public class SysServiceImpl implements SysService {

    /**
     * 根据内容生成二维码
     *
     * @param form     二维码数据表单
     * @param response 返回数据载体
     */
    @Override
    public void createQRCode(QRCodeForm form, HttpServletResponse response) {
        try {
            if (StrUtil.isNotBlank(form.getFilePath())) {
                ImageUtil.generateQRToFile(form.getContent(), form.getWidth(), form.getHeight(), form.getFilePath());
            }
            ImageUtil.generateQRToStream(form.getContent(), form.getWidth(), form.getHeight(), response);
        } catch (Exception e) {
            throw new CommonException(e.getMessage());
        }
    }

    /**
     * 根据网络文件地址下载到本地
     *
     * @param form 网络文件下载到本地提交表单
     */
    @Override
    public void getWebFile(WebFileForm form) {
        FileUtil.webFileToLocal(form.getWebFile(), form.getLocalPath());
    }

    @Resource
    private OssFeignClient ossFeignClient;

    @Resource
    private SysUserInfoMapper sysUserInfoMapper;

    /**
     * 测试API接口
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object testApi() {

        List<String> allUserId = sysUserInfoMapper.getAllUserId();
        System.err.println(allUserId);

        List<SysUserInfo> allUser = sysUserInfoMapper.getAllUser();
        System.out.println(allUser);

        return "stringResult";
    }

}
